// 健康数据分析工具函数

/**
 * 计算BMI指数
 * @param {number} weight - 体重(kg)
 * @param {number} height - 身高(cm)
 * @returns {object} BMI值和状态
 */
export function calculateBMI(weight, height) {
  const heightInMeters = height / 100;
  const bmi = weight / (heightInMeters * heightInMeters);
  
  let status = '';
  let statusColor = '';
  
  if (bmi < 18.5) {
    status = '偏瘦';
    statusColor = 'text-blue-500';
  } else if (bmi < 24) {
    status = '正常';
    statusColor = 'text-green-500';
  } else if (bmi < 28) {
    status = '偏胖';
    statusColor = 'text-yellow-500';
  } else {
    status = '肥胖';
    statusColor = 'text-red-500';
  }
  
  return {
    value: bmi.toFixed(1),
    status,
    statusColor,
    normal: bmi >= 18.5 && bmi < 24
  };
}

/**
 * 分析血压状态
 * @param {number} systolic - 收缩压
 * @param {number} diastolic - 舒张压
 * @returns {object} 血压状态和建议
 */
export function analyzeBloodPressure(systolic, diastolic) {
  let status = '';
  let statusColor = '';
  let advice = '';
  
  if (systolic < 120 && diastolic < 80) {
    status = '正常';
    statusColor = 'text-green-500';
    advice = '血压正常，请继续保持健康的生活方式';
  } else if (systolic < 130 && diastolic < 80) {
    status = '正常高值';
    statusColor = 'text-yellow-500';
    advice = '血压处于正常高值，建议注意饮食和运动';
  } else if (systolic < 140 || diastolic < 90) {
    status = '1级高血压';
    statusColor = 'text-orange-500';
    advice = '轻度高血压，建议改善生活方式并定期监测';
  } else if (systolic < 180 || diastolic < 120) {
    status = '2级高血压';
    statusColor = 'text-red-500';
    advice = '中度高血压，建议及时就医并遵医嘱治疗';
  } else {
    status = '高血压危象';
    statusColor = 'text-red-700';
    advice = '血压过高，请立即就医';
  }
  
  return {
    status,
    statusColor,
    advice,
    normal: systolic < 120 && diastolic < 80
  };
}

/**
 * 分析心率状态
 * @param {number} heartRate - 心率
 * @param {number} age - 年龄
 * @returns {object} 心率状态
 */
export function analyzeHeartRate(heartRate, age) {
  let status = '';
  let statusColor = '';
  let advice = '';
  
  // 根据年龄计算最大心率
  const maxHeartRate = 220 - age;
  const minNormal = 60;
  const maxNormal = 100;
  
  if (heartRate < minNormal) {
    status = '心动过缓';
    statusColor = 'text-blue-500';
    advice = '心率偏低，如有不适请咨询医生';
  } else if (heartRate > maxNormal) {
    status = '心动过速';
    statusColor = 'text-red-500';
    advice = '心率偏高，建议休息并观察';
  } else {
    status = '正常';
    statusColor = 'text-green-500';
    advice = '心率正常，请继续保持';
  }
  
  return {
    status,
    statusColor,
    advice,
    normal: heartRate >= minNormal && heartRate <= maxNormal,
    maxHeartRate
  };
}

/**
 * 生成健康建议
 * @param {object} healthData - 健康数据
 * @returns {array} 健康建议列表
 */
export function generateHealthAdvice(healthData) {
  const advices = [];
  
  // BMI建议
  if (healthData.bmi) {
    const bmi = calculateBMI(healthData.bmi.weight, healthData.bmi.height);
    if (!bmi.normal) {
      advices.push({
        type: 'warning',
        title: 'BMI指数异常',
        content: `您的BMI指数为${bmi.value}，属于${bmi.status}状态，建议调整饮食结构并增加运动量。`,
        priority: 'high'
      });
    }
  }
  
  // 血压建议
  if (healthData.bloodPressure) {
    const bp = analyzeBloodPressure(healthData.bloodPressure.systolic, healthData.bloodPressure.diastolic);
    if (!bp.normal) {
      advices.push({
        type: 'warning',
        title: '血压异常',
        content: bp.advice,
        priority: 'high'
      });
    }
  }
  
  // 心率建议
  if (healthData.heartRate && healthData.age) {
    const hr = analyzeHeartRate(healthData.heartRate, healthData.age);
    if (!hr.normal) {
      advices.push({
        type: 'warning',
        title: '心率异常',
        content: hr.advice,
        priority: 'medium'
      });
    }
  }
  
  // 通用建议
  advices.push({
    type: 'info',
    title: '保持健康生活方式',
    content: '建议每天进行30分钟中等强度运动，保持充足睡眠，饮食均衡。',
    priority: 'low'
  });
  
  return advices.sort((a, b) => {
    const priorityOrder = { high: 3, medium: 2, low: 1 };
    return priorityOrder[b.priority] - priorityOrder[a.priority];
  });
}

/**
 * 计算健康评分
 * @param {object} healthData - 健康数据
 * @returns {object} 健康评分和等级
 */
export function calculateHealthScore(healthData) {
  let score = 100;
  let issues = [];
  
  // BMI评分
  if (healthData.bmi) {
    const bmi = calculateBMI(healthData.bmi.weight, healthData.bmi.height);
    if (!bmi.normal) {
      score -= 15;
      issues.push('BMI异常');
    }
  }
  
  // 血压评分
  if (healthData.bloodPressure) {
    const bp = analyzeBloodPressure(healthData.bloodPressure.systolic, healthData.bloodPressure.diastolic);
    if (!bp.normal) {
      score -= 20;
      issues.push('血压异常');
    }
  }
  
  // 心率评分
  if (healthData.heartRate && healthData.age) {
    const hr = analyzeHeartRate(healthData.heartRate, healthData.age);
    if (!hr.normal) {
      score -= 10;
      issues.push('心率异常');
    }
  }
  
  let grade = '';
  if (score >= 90) {
    grade = '优秀';
  } else if (score >= 80) {
    grade = '良好';
  } else if (score >= 70) {
    grade = '一般';
  } else if (score >= 60) {
    grade = '需改善';
  } else {
    grade = '较差';
  }
  
  return {
    score: Math.max(0, score),
    grade,
    issues,
    recommendations: generateHealthAdvice(healthData)
  };
}